import {EntityRef} from '../entity/entity-ref';
import {StringUtils} from './string-utils';

export class ParamUtils {
    public static toStringWithAnnotation(paramRef: EntityRef): string {
        const content = [];
        if (paramRef.annotation) {
            content.push(paramRef.annotation);
            if (paramRef.annotationParams && paramRef.annotationParams.length > 3) {
                const annotationParams = JSON.parse(paramRef.annotationParams);
                content.push('(');
                const keys = Object.keys(annotationParams);
                if (keys.length === 1 && keys.indexOf('value') !== -1) {
                    const value = ParamUtils.formatValue(annotationParams['value']);
                    content.push(value);
                } else {
                    const params = [];
                    keys.forEach(function (key) {
                        const value = ParamUtils.formatValue(annotationParams[key]);
                        params.push(`${key}=${value}`);
                    });
                    content.push(params.join(', '));
                }
                content.push(')');
            }
            content.push(' ');
            content.push(StringUtils.toSimpleTypeName(paramRef.entityName));
            content.push(' ');
        }

        content.push(StringUtils.toSimpleTypeName(paramRef.name));
        return content.join('');
    }

    private static formatValue(value: any): string {
        if (typeof (value) === 'string') {
            return `"${value}"`;
        } else {
            return value;
        }
    }
}
